package com.pro.app.controller;


import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro.core.util.ResponseUtil;
import com.pro.core.validator.NotNullValidator;
import com.pro.db.domain.*;
import com.pro.db.enums.EnumContentType;
import com.pro.db.service.itf.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.baidu.unbiz.fluentvalidator.ResultCollectors.toComplex;

/**
 * <p>
 * 首页 前端控制器
 * </p>
 *
 * @author wanglei
 * @since 2019-12-21
 */
@RestController
@RequestMapping("/app/bbsSite")
public class AppBbsSiteController {
    private final Log logger = LogFactory.getLog(AppBbsSiteController.class);

    @Autowired
    private IBbsSiteService bbsSiteService;

    @Autowired
    private IBbsForumService bbsForumService;

    @Autowired
    private IGuopuBannerService bannerService;

    /**
     * @apiDefine bbsSite 站点接口
     *
     */

    /**
     * @api {GET} /app/bbsSite/list 3.1 获取站点列表
     * @apiName bbsSite list
     * @apiGroup bbsSite
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} [page] 页码
     * @apiParam {int} [size] 每页数量
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        Page<BbsSite> pageVo = new Page<>();
        pageVo.setCurrent(page);
        pageVo.setSize(size);
        IPage<BbsSite> pList = bbsSiteService.page(pageVo,
                new QueryWrapper<BbsSite>()
                        .eq("deleted",0)
                        .orderByDesc("add_time"));

        Map<String, Object> data = new HashMap<>();
        data.put("total", pList.getTotal());
        data.put("items", pList.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * @api {GET} /app/bbsSite/listAll 3.2 获取站点列表(包含版块信息)
     * @apiName bbsSite listAll
     * @apiGroup bbsSite
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} [page] 页码
     * @apiParam {int} [size] 每页数量
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @GetMapping("/listAll")
    public Object listAll(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        Page<BbsSite> pageVo = new Page<>();
        pageVo.setCurrent(page);
        pageVo.setSize(size);
        IPage<BbsSite> pList = bbsSiteService.page(pageVo,
                new QueryWrapper<BbsSite>()
                        .eq("deleted",0)
                        .orderByDesc("add_time"));

        for(BbsSite site:pList.getRecords()){
            List<BbsForum> forums = bbsForumService
                    .list(new QueryWrapper<BbsForum>()
                    .eq("site_id",site.getId())
                    .eq("pid",0)
                    .eq("deleted",0)
                    .orderByDesc("push_sort"));
            for(BbsForum mf:forums){
                List<BbsForum> subs = bbsForumService
                        .list(new QueryWrapper<BbsForum>()
                                .eq("pid",mf.getId())
                                .eq("deleted",0));
                mf.setSubForumList(subs);
            }
            site.setForumList(forums);
        }

        Map<String, Object> data = new HashMap<>();
        data.put("total", pList.getTotal());
        data.put("items", pList.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * @api {GET} /app/bbsSite/bannerSite 3.3 获取站点轮播图
     * @apiName bbsSite bannerSite
     * @apiGroup bbsSite
     * @apiVersion 1.0.0
     * @apiDescription 接口负责人-王雷
     *
     * @apiParam {int} siteId 站点ID
     *
     * @apiSuccess {int} errno 结果码 0:正常返回,-1服务异常
     * @apiSuccess {String} errmsg 前端提示消息
     * @apiSuccess {Object} data 结果集
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
    {
     *     "errno": 0,
     *     "errmsg": "成功"
     * }
     */
    @GetMapping("/bannerSite")
    public Object bannerSite(@NotNull Integer siteId) {
        List<GuopuBanner> pList = bannerService.list(
                new QueryWrapper<GuopuBanner>()
                        .eq("site_id",siteId)
                        .eq("deleted",0)
                        .eq("push_status",1)
                        .orderByDesc("push_sort"));

        Map<String, Object> data = new HashMap<>();
        data.put("total", pList.size());
        data.put("items", pList);
        return ResponseUtil.ok(data);
    }

}

